package com.leyou.item.service;

import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author CHERRY
 * @date 2018-09-04-14:28
 */
@Service
public class SpecificationService {
    @Autowired
    private SpecGroupMapper specGroupMapper;
    @Autowired
    private SpecParamMapper specParamMapper;

    /**
     * 查询分组
     *
     * @param cid
     * @return
     */
    public List<SpecGroup> querySpecGroupByCid(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        //通用mapper 查询分组
        List<SpecGroup> specGroups = specGroupMapper.select(specGroup);
        return specGroups;
    }

    /**
     * 查询组内规格参数
     *
     * @return
     */
    public List<SpecParam> querySpecParamByGid(Long gid) {
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        //通用mapper 查询规格参数
        return this.specParamMapper.select(specParam);
    }

    /**
     * 更新分组数据
     *
     * @param specGroup
     */
    public void editSpecGroupByGroup(SpecGroup specGroup) {

        this.specGroupMapper.updateByPrimaryKeySelective(specGroup);
    }

    /**
     * 添加分组
     *
     * @param specGroup
     */
    public void saveSpecGroup(SpecGroup specGroup) {
        //添加分组数据
        this.specGroupMapper.insertSelective(specGroup);


    }

    /**
     * 删除分组和规格参数
     *
     * @param cid 分类
     * @param gid 分组
     */
    public void deleteSpecGroup(Long cid, Long gid) {
        //删除分组数据
        this.specGroupMapper.deleteByPrimaryKey(gid);
        //删除规格参数数据
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        //通用mapper删除
        this.specParamMapper.delete(specParam);
    }

    /**
     * 添加规格参数
     *
     * @param specParam
     */
    public void saveSpecParam(SpecParam specParam) {
        this.specParamMapper.insertSelective(specParam);
    }

    /**
     * 根据id删除规格参数
     *
     * @param id
     */
    public void deleteSpecParam(Long id) {
        this.specParamMapper.deleteByPrimaryKey(id);
    }

    /**
     * 编辑更新规格参数
     *
     * @param specParam
     */
    public void updateSpecParam(SpecParam specParam) {
        this.specParamMapper.updateByPrimaryKeySelective(specParam);
    }

    /**
     * 查询规格参数
     *
     * @param cid
     * @param gid
     * @param searching
     * @param generic
     * @return
     */
    public List<SpecParam> querySpecParams(Long cid, Long gid, Boolean searching, Boolean generic) {
        //通用mapper查询 ,参数如为null 不作为查询条件
        SpecParam specParam = new SpecParam();
        specParam.setCid(cid);
        specParam.setGroupId(gid);
        specParam.setSearching(searching);
        specParam.setGeneric(generic);
        List<SpecParam> specParams = this.specParamMapper.select(specParam);
        return specParams;
    }

    /**
     * 根据cid查询组和规格参数数据
     *
     * @param cid
     * @return
     */
    public List<SpecGroup> querySpecGroupAndSpecParamsByCid(Long cid) {
        List<SpecGroup> specGroups = this.querySpecGroupByCid(cid);
        specGroups.forEach(specGroup -> {
            //调用查询规格参数方法
            specGroup.setSpecParams(this.querySpecParams(cid, specGroup.getId(), null, null));
        });
        return specGroups;
    }
}
